package com.joysuch.wwyt.datadump.biz.convertor;

import cn.zlg.common.datadump.annotation.DataDumpField;
import cn.zlg.common.datadump.convertor.FieldValueConvert;
import cn.zlg.common.util.CollectionUtils;
import com.alibaba.fastjson.JSON;
import com.joysuch.wwyt.core.enums.RriskFactorEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author ZhaoQiuWei
 * @Date 2021/10/26 15:41
 */
@Component
public class ImportRiskFactorConvertor implements FieldValueConvert<String, String> {
    @Override
    public String convert(Object bean, String value, DataDumpField dataField) {
        if (StringUtils.isNotBlank(value)) {
            value = value.trim().replaceAll("（", "(").replaceAll("）", ")").replaceAll("，", ",");
            List<String> list = Arrays.asList(value.split(","));
            for (String str : list) {
                //内容有一条不符合则返回null;
                if (!RriskFactorEnum.checkByValue(str)) {
                    throw new IllegalArgumentException("请填写正确的风险要素:人(人的不安全行为) 物(物的不安全状态) 环(环境因素) 管(管理因素)");
                }
            }
            if (CollectionUtils.isNotEmpty(list)) {
                List<String> collect = list.stream().distinct().collect(Collectors.toList());
                return JSON.toJSONString(collect);
            }
        }
        return null;
    }
}
